(function($, window, document) {

	$.fn.tree = function(options) {
		var o = $.extend({}, {
			id : "",

			setting : null,
			actions : {
				cusEditUrl : null,
				cusCreateUrl : null,
				cusViewUrl : null,
				cusDeleteUrl : null,
				afterActionsSuccess : null,
				beforeCreateAction : true,
				beforeDeleteFun : null,
				beforeAddHoverDomeFun : true,
				beforeDeleteMessage : "",
				showLv1DeleteBtn:true

			},
			cusDialog : {
				cusDialogWidth : null,
				cusDialogHeight : null,
				cusDialogSize : null,
				cusDialogMax : null
			}
		}, options);

		var self = this, $element = $(this), _openDialog = function(title, url, parentNode) {
			if (isperp.openDialog)
				isperp.openDialog({
					title : title,
					width : o.cusDialogWidth,
					height : o.cusDialog.cusDialogHeight,
					windowSize : o.cusDialog.cusDialogSize,
					max : o.cusDialog.cusDialogMax,
					contentUrl : url,
					close : function() {
						// $result.reload(parentNode);
					}
				});

		},

		_createTree = function() {
			if (o.setting.view.addHoverDom) {
				o.setting.view.addHoverDom = $result.addHoverDom;
			}

			if (o.setting.view.removeHoverDom) {
				o.setting.view.removeHoverDom = $result.removeHoverDom;
			}
			return $.fn.zTree.init($("#" + o.id), o.setting);
		}, _afterSuccess = function(self, o) {
			if (o.actions.afterActionsSuccess) {
				o.actions.afterActionsSuccess();
			}
			$result.reload();
		}, $result = {
			addHoverDom : function(treeId, treeNode) {

				if (typeof (o.actions.beforeAddHoverDomeFun) == "function") {
					var beforeAddHoverDomeResult = o.actions.beforeAddHoverDomeFun(treeId, treeNode);

					if (beforeAddHoverDomeResult == false) {
						return false;
					}
				}

				var nodeUI = $('#' + treeNode.tId + '_a');
				var operBar = nodeUI.children().eq(2);
				if (operBar.length > 0) {
					operBar.css('visibility', 'visible');
					return;
				}
				operBar = $('<span>').css('margin-left', '8px').appendTo(nodeUI);
				if (o.actions.cusCreateUrl || o.actions.cusEditUrl) {
					$('<a class="ico ico-plus hover" title="添加子节点"></a>').click(function() {
						$result.zTree.selectNode(treeNode, true);
						$result.create(undefined, treeNode);
					}).appendTo(operBar);
				}
				

				if (o.actions.cusEditUrl && treeNode.id) {
					$('<a class="ico ico-edit hover" title="编辑" style="margin:0 6px;"></a>').click(function() {
						$result.zTree.selectNode(treeNode, true);
						$result.edit(treeNode);
					}).appendTo(operBar);
				}
				if (o.actions.cusDeleteUrl && (treeNode.parentId || o.actions.showLv1DeleteBtn)) {
					$('<a class="ico ico-remove hover" title="删除"></a>').click(function() {
						$result.zTree.selectNode(treeNode, true);
						$result.deleteItem(treeNode);
					}).appendTo(operBar);
				}
			},
			removeHoverDom : function(treeId, treeNode) {
				$('#' + treeNode.tId + '_a').children(':eq(2)').css('visibility', 'hidden');

			},
			reload : function(parentNode) {
				$result.beforeReloadSelectedNode = $result.getSelectedNode();
				$result.zTree.reAsyncChildNodes(parentNode, "refresh");
			},
			afterAsyncSuccess : function(selectedId) {
				if ($result.beforeReloadSelectedNode) {
					var nnode = $result.zTree.getNodeByParam("id", $result.beforeReloadSelectedNode.id);
					if (nnode)
						$result.zTree.selectNode(nnode, true);
				}
			},
			getSelectedNode : function() {
				var nodes = $result.zTree.getSelectedNodes();
				if (nodes == undefined)
					return undefined;
				return nodes[0];
			},
			create : function(params, treeNode) {
				var beforeCreateActionResult = true;
				if (o.actions.beforeCreateAction && typeof (o.actions.beforeCreateAction) == "function") {
					beforeCreateActionResult = o.actions.beforeCreateAction(params, treeNode);
				}

				var node = null;
				if (treeNode) {
					node = treeNode;
				} else {
					var nodes = $result.zTree.getSelectedNodes();

					if (!nodes || nodes == "" || nodes.length == 0) {
						isperp.alert("请选择一个节点");
						return false;
					}
					node = nodes[0];
				}
				params = (params == undefined ? beforeCreateActionResult : (params + (beforeCreateActionResult ? "" : "&" + beforeCreateActionResult)));

				if (!beforeCreateActionResult) {
					return false;
				}
				params += "&parentId=" + node.id + "&pcontrolid=" + o.id;
				_openDialog("新增下一级节点", (o.actions.cusCreateUrl || o.actions.cusEditUrl) + "?" + params, node);
			},
			edit : function(treeNode) {
				var node = null;
				if (treeNode) {
					node = treeNode;
				} else {
					var nodes = $result.zTree.getSelectedNodes();

					if (!nodes || nodes == "" || nodes.length == 0) {
						isperp.alert("请选择一个节点");
						return false;
					}
					node = nodes[0];
				}

				if (o.actions.cusEditUrl.indexOf("?") > -1) {
					_openDialog("编辑节点", (o.actions.cusEditUrl + "&id=" + node.id + "&pcontrolid=" + o.id), node);
				} else {
					_openDialog("编辑节点", (o.actions.cusEditUrl + "?id=" + node.id + "&pcontrolid=" + o.id), node);
				}

			},
			remove : function(treeNode) {
				$result.deleteItem(treeNode)
			},
			deleteItem : function(treeNode) {

				if (!o.actions.cusDeleteUrl || o.actions.cusDeleteUrl == "") {
					isperp.alert("无法删除，请联系管理员！");
					return;
				}
				var result = true;
				if (o.actions.beforeDeleteFun && typeof (o.actions.beforeDeleteFun) == "function") {
					result = o.actions.beforeDeleteFun($result);
				}
				if (typeof (result) != "string" || result == "") {
					result = o.actions.beforeDeleteMessage;
				}
				isperp.confirmAlert(o.actions.beforeDeleteMessage, {
					yes : function() {
						var node = null;
						if (treeNode) {
							node = treeNode;
						} else {
							var nodes = $result.zTree.getSelectedNodes();

							if (!nodes || nodes == "" || nodes.length == 0) {
								isperp.alert("请选择一个节点");
								return false;
							}
							node = nodes[0];
						}

						$.post(o.actions.cusDeleteUrl, {
							id : node.id
						}, function(data, textStatus) {
							if (data.success) {
								$result.zTree.removeNode(node);
							} else {
								isperp.alert(data.data);
							}
						}, "json");
						if (o.actions.afterActionsSuccess) {
							o.actions.afterActionsSuccess();
						}

					}
				});
			},
			beforeReloadSelectedNode : null,
			expandAll : function() {
				$result.expandAll();
			}

		};
		$result.zTree = _createTree();
		return $result;
	}

})(jQuery, this);
